package com.boyon.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boyon.common.domain.UserSession;
import com.boyon.common.exection.BusinessException;
import com.boyon.common.log.annotation.MethodFlag;
import com.boyon.common.status.AccountType;
import com.boyon.common.status.StatusType;
import com.boyon.common.tools.ObjectTool;
import com.boyon.common.tools.StringTool;
import com.boyon.hbm.criteria.Query;
import com.boyon.service.AbstractService;
import com.boyon.system.entity.Menu;
import com.boyon.system.entity.Role;
import com.boyon.system.entity.RoleManager;

/**
 * ClassName:RoleService <br/>
 * Function: role service. <br/>
 * Date: 2018年3月15日 上午12:34:53 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Service
public class RoleService extends AbstractService {

	@Autowired
	private MenuService menuService;

	/**
	 * 
	 * createRole:创建角色信息. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@MethodFlag(key = "")
	@Transactional
	public Long createRole(Role role, AccountType accountType, UserSession userSession) {
		role.setCreateTime(new Date());
		role.setUpdateTime(new Date());
		role.setStatus(StatusType.start.getStatus());
		role.setIsSystem(0);
		role.setRelationId(userSession.getAccountId());
		if (AccountType.system == accountType) {
			role.setIsSystem(1);
		}
		generalDao.saveOrUpdate(role);
		if (AccountType.system == accountType) {			
			RoleManager roleManager = new RoleManager();
			roleManager.setRoleId(role.getRoleId());
			roleManager.setMenu(role.getMenu());
			generalDao.saveOrUpdate(roleManager);
		}
		return role.getRoleId();
	}

	/**
	 * 
	 * modifyRole:编辑角色信息. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@MethodFlag(key = "")
	@Transactional
	public Long modifyRole(Role role, AccountType accountType, UserSession userSession) {
		Role entity = findByRoleId(role.getRoleId());
		entity.setUpdateTime(new Date());
		entity.setRoleName(role.getRoleName());
		entity.setSummary(role.getSummary());
		entity.setRemark(role.getRemark());
		entity.setAuthority(role.getAuthority());
		generalDao.saveOrUpdate(entity);
		if (AccountType.system == accountType) {
			RoleManager roleManager = generalDao.get(RoleManager.class,
					role.getRoleId());
			roleManager.setMenu(role.getMenu());
			generalDao.saveOrUpdate(roleManager);
		}
		return entity.getRoleId();
	}

	/**
	 * 
	 * deleteRole:删除角色. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	@MethodFlag(key = "")
	@Transactional
	public Long deleteRole(Long roleId) {
		Role entity = findByRoleId(roleId);
		entity.setStatus(StatusType.del.getStatus());
		entity.setUpdateTime(new Date());
		generalDao.saveOrUpdate(entity);
		return entity.getRoleId();
	}

	/**
	 * 
	 * findByRoleId:角色唯一标识查找角色. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Role findByRoleId(Long roleId) {
		if (!ObjectTool.isNotNull(roleId)) {
			throw BusinessException.builder(0, "角色ID没有");
		}
		Role role = generalDao.get(Role.class, roleId);
		if (role == null) {
			throw BusinessException.builder(0, "角色不存在");
		}
		if (role.getStatus() == StatusType.del.getStatus()) {
			throw BusinessException.builder(0, "角色已删除");
		}
		return role;
	}

	/**
	 * 
	 * findMenuByRoleId:查询角色菜单权限. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Menu findMenuByRoleId(Long loginRoleId, Long roleId,
			Integer accountType) {
		List<Long> menuIds = null;
		if (ObjectTool.isNotNull(roleId)) {
			menuIds = findAuthorityByRoleId(roleId); // 角色包含的权限
		}
		if (!ObjectTool.isNotNull(menuIds)) {
			menuIds = new ArrayList<>();
		}
		return menuService.query(loginRoleId, roleId, menuIds,
				AccountType.getType(accountType));
	}

	/**
	 * 
	 * findMenuByManager:查询管理权限可使用菜单. <br/>
	 * 
	 * @author sunshulin
	 * @param loginRoleId
	 * @param roleId
	 * @param accountType
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Menu findMenuByManager(Long loginRoleId, Long roleId,
			Integer accountType) {
		List<Long> menuIds = null;
		if (ObjectTool.isNotNull(roleId)) {
			Role role = findByRoleId(roleId);
			RoleManager roleManager = generalDao.get(RoleManager.class,
					role.getRoleId());
			if (roleManager != null) {
				// 查询管理权限可使用菜单
				if (StringTool.isNotNull(roleManager.getMenu())) {
					menuIds = StringTool.converStringToLong(
							roleManager.getMenu().split(","));
				}
			}
		}
		if (!ObjectTool.isNotNull(menuIds)) {
			menuIds = new ArrayList<>();
		}
		return menuService.findRoleManagerByUserMenu(menuIds);
	}

	/**
	 * 
	 * filterMenuByRoleId:获取角色包含的菜单,过滤不存在菜单. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public Menu filterMenuByRoleId(Long roleId) {
		List<Menu> menus = menuService.queryMenu();
		List<Long> menuIds = findAuthorityByRoleId(roleId);
		menus = menuService.filter(menus, menuIds);
		return menuService.tree(menus);
	}

	/**
	 * 
	 * filterButtonByRoleId:获取角色包含的按钮,过滤不存在按钮. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public List<Menu> filterButtonByRoleId(Long roleId) {
		// Role entity = findByRoleId(roleId);
		// List<Menu> menus = menuService.queryButton();
		// List<Long> menuIds = findAuthorityByRoleId(entity.getRoleId());
		// return menuService.filter(menus, menuIds);
		return null;
	}

	/**
	 * 
	 * findAuthorityByRoleId:查询获取角色权限. <br/>
	 * 
	 * @author sunshulin
	 * @param roleId
	 * @return
	 * @since JDK 1.8
	 */
	public List<Long> findAuthorityByRoleId(Long roleId) {
		Role role = findByRoleId(roleId);
		String authority = role.getAuthority();
		List<Long> menuIds = null;
		if (StringTool.isNotNull(authority)) {
			menuIds = StringTool.converStringToLong(authority.split(","));
		}
		return menuIds;
	}

	/**
	 * 
	 * query:查询角色列表 <br/>
	 * 
	 * @author sunshulin
	 * @param query
	 * @return
	 * @since JDK 1.8
	 */
	@Transactional(readOnly = true)
	public List<Role> query(Query query) {
		List<Role> list = generalDao.query(Role.class, query);
		return list;
	}
}